<?php 


include('databaseconnection.php');
//function csv_to_array($input, $delimiter=',') 
//{ 

	  if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
if (file_exists("Upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
	
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "Upload/" . $_FILES["file"]["name"]);


	  }
    $header = null; 
    $data = array(); 
    $csvData = str_getcsv(file_get_contents('Upload/'.$_FILES["file"]["name"]), "\n"); 
    $delimiter = ",";
    foreach($csvData as $csvLine){ 
        if(is_null($header)) $header = explode($delimiter, $csvLine); 
        else{ 
            $sql="INSERT INTO terms VALUES(";
            $items = explode($delimiter, $csvLine); 
            
            for($n = 0, $m = count($header); $n < $m; $n++){ 
               // $prepareData[$header[$n]] = $items[$n]; 
				//Mysql 
				if($n != $m-1) 
					$sql.= "'".$items[$n]."',";
				else
					$sql.= "'".$items[$n]."'";
            } 
			$sql.= ")";
			echo $sql;
            mysql_query($sql);
           // $data[] = $prepareData; 
        } 
    } 
    
   // return $data; 
//} 

//----------------------------------- 
// 
//Usage: 
mysql_close($con);


//echo "<pre>";
//print_r($data);

?> 